/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package heartinjava;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.*;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author TIENDAT
 */

public class ConnectClient extends KhoiTao {
    
    ConnectClient(final HeartInJava m_main, final KhungBanCo gui) 
    {
        this.m_Main = m_main;
        this.gui = gui;
    }

    @Override
    public void Enter() 
    {
        //--xóa toàn bộ màn hình
        gui.container.removeAll();
        //--hình nền
        URL path = getClass().getResource("resources/bg1_2.jpg");
        JLabel bg= new JLabel(new ImageIcon(path));
        bg.setBounds(gui.container.getX(), gui.container.getX(),gui.container.getWidth(), gui.container.getHeight());
        gui.container.setBackground(Color.WHITE);
        gui.container.add(bg);
        Font f1 = new Font(Font.DIALOG, Font.BOLD, 12);
        JLabel ip = new JLabel("Host IP : ");
        ip.setFont(f1);
        ip.setForeground(Color.BLUE);
        ip.setBounds(300, 170, 60, 30);

        final JTextField textip = new JTextField();
        String lc = null;
        try {
            InetAddress local = InetAddress.getLocalHost();
            String hname = local.getHostAddress();
            String []sname = hname.split("\\.");
            if (sname.length>3)
            {
                lc = sname[0]+"."+sname[1] +"."+ sname[2] +"."+sname[3];
            }
        } catch (UnknownHostException ex) {
            Logger.getLogger(ConnectClient.class.getName()).log(Level.SEVERE, null, ex);
        }
        if(lc!=null && !lc.equals("127.0.0."))
        {
            textip.setText(lc);
        }
        else
        {
            textip.setText("127.0.0.1");
        }
        
        textip.setBounds(350, 170, 150, 30);

        gui.container.add(ip,0);
        gui.container.add(textip,0);

        JButton btnConnect = new JButton("Join");
        btnConnect.setBounds(510, 170, 60, 30);

        final JOptionPane jop = new JOptionPane("Warning");
        jop.setBounds(300, 300, 300, 300);

        btnConnect.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {
                if (textip.getText() == null || (textip.getText() == null ? "" == null : textip.getText().equals(""))) 
                {
                    jop.showMessageDialog(jop, "Vui lòng nhập host IP");
                } 
                else 
                {
                    final String Ip = textip.getText();
                    final int portclient = KhaiBao.DEFAULTPORT;
                    Client testConnect = new Client(Ip, portclient, m_Main);
                    if (testConnect.m_Connect == true)
                    {
                        testConnect.stopThread();
                        m_Main.SwitchState(KhaiBao.WAIT);
                        m_Main.IsClient(Ip, portclient);
                    }
                    else
                    { 
                        jop.showMessageDialog(jop, "Không tìm thấy host....");
                    }
                }
            }
        });
        
        //--nút thoát
        JButton Done = new JButton("Done");
        Done.setBounds(380, 380, 140, 30);
        Done.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) 
            {
                m_Main.SwitchState(KhaiBao.MENU);
            }
        });
        gui.container.add(Done,0);
        gui.container.add(btnConnect,0);
        gui.repaint();
    }
}
